import { Schema, h } from "koishi"
export const name = "拉群广播"
export const inject = { optional: ["database"] }
export const Config = Schema.object({
  cids: Schema.array(String),
})
export async function apply(ctx, config) {
  ctx.on('guild-request', async session => {
    ctx.broadcast(config.cids, h.i18n("lnnbot.拉群广播.guild-request", [await session.bot.getGuild(session.event.guild.id).then(({ name }) => name && `${name} (${session.gid})`, () => null) || session.gid, session.userId]))
  })
  ctx.on('guild-added', async session => {
    ctx.broadcast(config.cids, h.i18n("lnnbot.拉群广播.guild-added", [await session.bot.getGuild(session.event.guild.id).then(({ name }) => name && `${name} (${session.gid})`, () => null) || session.gid]))
  })
  ctx.on('guild-deleted', async session => {
    ctx.broadcast(config.cids, h.i18n("lnnbot.拉群广播.guild-deleted", [await session.bot.getGuild(session.event.guild.id).then(({ name }) => name && `${name} (${session.gid})`, () => null) || session.gid]))
    if (ctx.platform === "onebot") {
      ctx.logger.info("remove assignee for group", session.gid)
      ctx.database.update("channel", { platform: session.platform, id: session.guildId }, { assignee: null })
    }
  })
  ctx.i18n.define("zh-CN", "lnnbot.拉群广播", {
    "guild-request": "LNNBot 被 {1} 邀请加入群组 {0}",
    "guild-added": "LNNBot 已加入群组 {0}",
    "guild-deleted": "LNNBot 已离开群组 {0}",
  })
}
